import { CheckboxValueType } from "antd/es/checkbox/Group";
import ApiClient from ".";
import { objectWithEmpty } from "@/utils/fun";

const api = new ApiClient("http://localhost:3000");

export interface Question {
  _id: number;
  type: string;
  difficulty: string;
  tags: string;
  question: string;
  options: string;
  analysis: string;
  answer: string;
  description: string;
}

export interface TagsItem {
  _id: number;
  tagName: string;
}

export interface CategoriesItem {
  _id: number;
  categoryName: string;
}

export interface PostsItem {
  _id: number;
  userID: string;
  title: string;
  content: string;
  tags: string;
}

export interface ForumsItem {
  _id: number;
  userID: string;
  title: string;
  content: string;
  categories: string;
}

export interface QuestionProps {
  index: number;
  value: string | CheckboxValueType[];
  handleCheck: (text: string | CheckboxValueType[]) => void;
  data: Question;
}

export interface Papers {
  _id: number;
  paperName: string;
  paperContent: string;
  rules: string;
  isOpen: string;
}

export interface Exams {
  _id: number;
  examName: string;
  paperID: string;
  startTime: number;
  endTime: number;
  userIDs: string;
}

// 获取题目
export async function getQuestion(params: any = {}) {
  return await api.post<Question[]>("/randomQuestions", params);
}

// 获取标签
export async function getTags() {
  return await api.post<TagsItem[]>("/tags");
}

// 获取分类
export async function getCategories() {
  return await api.post<CategoriesItem[]>("/categories");
}

// 获取博客
export async function getPosts(params: any = {}) {
  return await api.post<PostsItem[]>("/posts", objectWithEmpty(params));
}

// 获取帖子
export async function getForums(params: any = {}) {
  return await api.post<ForumsItem[]>("/forums", objectWithEmpty(params));
}

// 获取试卷
export async function getPapers(params: any = {}) {
  return await api.post<Papers[]>("/papers", objectWithEmpty(params));
}

// 获取个人考试
export async function getPersonalExams() {
  const userID = sessionStorage.getItem("userID");
  return await api.post<Exams[]>("/getPersonalExams", {
    userID: userID ? Number(userID) : undefined,
  });
}

// 获取考试试卷
export async function getNoAnswerPaper(params: any = {}) {
  const { paperID } = params;
  return await api.post<Question[]>("/getNoAnswerPaper", {
    paperID: paperID ? Number(paperID) : undefined,
  });
}
